package android.slc.appbase.api.slc.config;

import android.slc.appbase.R;
import android.slc.db.sp.DpManager;

import com.blankj.utilcode.util.StringUtils;

/**
 * Created by on the way on 2018/9/18.
 */

public class ApiConfig extends DpManager {

    private int pageSize = Value.DEF_PAGE_SIZE;

    private String serviceApiAddress;

    private static class Holder {
        private static final ApiConfig INSTANCE = new ApiConfig();
    }

    public static ApiConfig getInstance() {
        return ApiConfig.Holder.INSTANCE;
    }

    protected ApiConfig() {
        super(Config.SP_NAME);
    }

    public String getServiceApiAddress() {
        if (isApplyCacheApi()) {
            return getServiceApiAddressByDp(serviceApiAddress);
        }
        return serviceApiAddress;
    }

    public void setServiceApiAddress(String value) {
        setServiceApiAddressByDp(value, false);
        this.serviceApiAddress = value;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    //token
    public String getToken() {
        return dp.getString(Config.KEY_TOKEN);
    }

    public void setToken(String token) {
        dp.putValue(Config.KEY_TOKEN, token);
    }

    public static class Key {
        //token的key
        public static final String KEY_TOKEN = "Authorization";
        public static final String KEY_AUTO_SEARCH = "autoSearch";

    }

    public static class Value {
        //**********配置api地址******************************//
        public static final String API_DOMAIN_NAME = "domain-api";
        //**********系统中的默认参数**************************//
        //默认的每页个数
        public static final int DEF_PAGE_SIZE = 15;
        //**********错误码***********************************//
        //后台定义
        public static final int SUCCEED = 200;//成功
        public static final int BUSINESS_EXCEPTIONS = 400;//后台提示
        public static final int NORMAL_UNAUTHORIZED = 401;//请求未授权
        public static final int NORMAL_ERROR = 500;//后台提示
        public static final int A_KNOWN_ERROR = 501;//已知错误
        //客户端定义
        public static final int RESULT_UPLOAD_FAILURE = 30003;//上传文件失败
        public static final String RESULT_UPLOAD_FAILURE_MSG = StringUtils.getString(R.string.label_file_upload_by_file_failed);//返回结果list为空提示
    }


    //apply
    public boolean isApplyCacheApi() {
        return dp.getBoolean(Config.KEY_APPLY_CACHE_API);
    }

    public void setApplyCacheApi(boolean applyCacheApi) {
        dp.putValue(Config.KEY_APPLY_CACHE_API, applyCacheApi);
    }

    //service
    public int getServiceMode() {
        return getServiceMode(Config.CONFIG_MODE_HM);
    }

    public int getServiceMode(int defValue) {
        return dp.getInt(Config.KEY_SERVICE_MODE, defValue);
    }

    public void setServiceMode(int value) {
        dp.putValue(Config.KEY_SERVICE_MODE, value);
    }

    public String getServiceApiAddressByDp() {
        return getServiceApiAddressByDp(null);
    }

    public String getServiceApiAddressByDp(String defValue) {
        return dp.getString(Config.KEY_SERVICE_API_ADDRESS, defValue);
    }

    public void setServiceApiAddressByDp(String value) {
        setServiceApiAddressByDp(value, true);
    }

    public void setServiceApiAddressByDp(String value, boolean forceSave) {
        if (forceSave || StringUtils.isTrimEmpty(getServiceApiAddressByDp())) {
            dp.putValue(Config.KEY_SERVICE_API_ADDRESS, value);
        }
    }


    public static class Config {
        private final static String SP_NAME = "api_config";
        private final static String KEY_APPLY_CACHE_API = "applyCacheApi";
        private final static String KEY_SERVICE_MODE = "serviceMode";
        private final static String KEY_SERVICE_API_ADDRESS = "serviceApiAddress";
        private final static String KEY_TOKEN = "token";

        //手动
        public final static int CONFIG_MODE_HM = 0;
        //自动
        public final static int CONFIG_MODE_AUTO_CURRENT = 1;
        public final static int CONFIG_MODE_AUTO_LOCAL = 2;
    }
}